GLOBAL.setmetatable(env, { __index = function(t, k) return GLOBAL.rawget(GLOBAL, k) end })

------------------------------------------------------------------------------
---冲天刺落地伤害
local mydamage = TUNING.WATHGRITHR_SPEAR_DAMAGE
---需要添加动作的预制物
local mywuqi = "spear_wathgrithr"
---跳跃AOE落地范围
local aoefanwei = 4
---冷却时间
local lengque = TUNING.SPEAR_WATHGRITHR_LIGHTNING_LUNGE_COOLDOWN
---动作范围（装备上多大范围可以触发）
local actfanwei = 12

------------------------------------------------------------------------------
-- 基础矛冲天刺相关处理
local function BasicSpear_OnLeapPre(inst, doer, startpos, endpos)
    -- 检查是否在冷却中
    if inst.components.rechargeable and not inst.components.rechargeable:IsCharged() then
        return false
    end

    -- 处理冲天刺前的逻辑
    for _, v in pairs(TheSim:FindEntities(endpos.x, endpos.y, endpos.z, 4)) do
        if v.components and v.components.combat and v ~= doer then
            v.components.combat:GetAttacked(doer, mydamage)
        end
    end

    -- 开始冷却
    if inst.components.rechargeable then
        inst.components.rechargeable:Discharge(lengque)
    end
end

local function BasicSpear_OnLeap(inst, doer, startingpos, targetpos)
    -- 生成跳跃特效
    SpawnPrefab("superjump_fx"):SetTarget(inst)
    
    -- 生成落地冲击波效果
    local fx = SpawnPrefab("groundpound_fx")
    local x, y, z = targetpos:Get()
    fx.Transform:SetPosition(x, y, z)
    fx.Transform:SetScale(0.7, 0.7, 0.7)
end

-- 对基础矛(spear_wathgrithr)进行后初始化
AddPrefabPostInit(mywuqi, function(inst)
    -- 添加超级跳跃标签
    inst:AddTag("superjump")
    
    if not TheWorld.ismastersim then return inst end

    -- 添加aoeweapon_leap组件
    inst:AddComponent("aoeweapon_leap")
    inst.components.aoeweapon_leap:SetAOERadius(aoefanwei)
    inst.components.aoeweapon_leap:SetOnLeaptFn(BasicSpear_OnLeap)
    inst.components.aoeweapon_leap:SetOnPreLeapFn(BasicSpear_OnLeapPre)
    
    -- 添加可充能组件
    inst:AddComponent("rechargeable")
    inst.components.rechargeable:SetMaxCharge(lengque)
    
    -- 保存原始装备回调
    local oldonequipfn = inst.components.equippable.onequipfn
    local oldonunequipfn = inst.components.equippable.onunequipfn
    
    -- 重写装备回调
    inst.components.equippable:SetOnEquip(function(inst, owner)
        oldonequipfn(inst, owner)
        -- 启用跳跃技能
        ACTIONS.SPEARLEAP.distance = actfanwei
        oldonequipfn(inst, owner)
        owner:AddTag("aoeweapon_leap_user")
    end)
    
    -- 重写卸下回调
    inst.components.equippable:SetOnUnequip(function(inst, owner)
        oldonunequipfn(inst, owner)
        -- 禁用跳跃技能
        ACTIONS.SPEARLEAP.distance = nil
        oldonunequipfn(inst, owner)
        owner:RemoveTag("aoeweapon_leap_user") 
    end)
end)

-- 添加右键动作
AddAction("SPEARLEAP", "冲天刺", function(act)
    local weapon = act.invobject or act.doer.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
    if weapon and (weapon.prefab == "spear_wathgrithr") then
        -- 检查是否在冷却中
        if weapon.components.rechargeable and not weapon.components.rechargeable:IsCharged() then
            return false
        end
        -- 触发跳跃事件
        act.doer:PushEvent("combat_superjump", { targetpos = act:GetActionPoint(), weapon = weapon })
        return true 
    end
end)

-- 注册右键动作 
AddComponentAction("POINT", "aoeweapon_leap", function(inst, doer, pos, actions, right)
    if right and inst:HasTag("superjump") then
        -- 检查是否在冷却中
        if inst.components.rechargeable and inst.components.rechargeable:IsCharged() then
            table.insert(actions, ACTIONS.SPEARLEAP)
        end
    end
end)

-- 添加状态图动作处理
AddStategraphActionHandler("wilson", ActionHandler(ACTIONS.SPEARLEAP, "combat_superjump_start"))
AddStategraphActionHandler("wilson_client", ActionHandler(ACTIONS.SPEARLEAP, "combat_superjump_start"))